class Trie{
    constructor() {
        this.children = new Array(26);
        this.isEnd = false;
    }
    insert(world){
        let node = this;
        for(let i=0;i<world.length;++i){
            let char = world[i];
            let index = char.charCodeAt(0) - 97;
            if(node.children[index] === null){
                node.children[index] = new Trie();
            }
            node = node.children[index];
        }
        node.isEnd = true;
    }
    search(word){
        let node = this.searchPrefix(word);
        return node != null && node.isEnd;
    }
    startsWith(prefix){
        return this.searchPrefix(prefix) !== null;
    }
    searchPrefix(prefix){
        let node = this;
        for(let i = 0; i < prefix.length; ++i){
            let char = prefix[i];
            let index = char.charCodeAt(0);
            if(node.children[index] === null){
                return null;
            }
            node = node.children[index];
        }
        return node;
    }
}

